package org.fronczak.anita.berlinclock;

import org.fronczak.anita.berlinclock.model.BerlinClockTime;
import org.fronczak.anita.berlinclock.view.BerlinClockTimeTextRepresentation;

public class BerlinClockTimeController {

    public static final String ILLEGAL_INPUT_MESSAGE = "Illegal input format. Legal values are 00:00:00 up to 24:00:00. Minutes and seconds must be in <00,59> range inclusive";

    public static final String USAGE_INFO = "usage: \r\nProgram accepts exactly one argument which is time in HH:mm:ss format, for example 23:58:22";

    private static final String INPUT_STRING_PATTERN = "^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$";

    private BerlinClockTime berlinClockTime;

    public BerlinClockTimeController(String input) {
        if (input == null || !input.trim().matches(INPUT_STRING_PATTERN)) {
            throw new IllegalArgumentException(ILLEGAL_INPUT_MESSAGE);
        }

        String[] parts = input.split(":");
        // if input matches pattern there is no nedd for checking if there are 3 parts after split
        int hours = Integer.parseInt(parts[0]);
        int minutes = Integer.parseInt(parts[1]);
        int seconds = Integer.parseInt(parts[2]);
        berlinClockTime = new BerlinClockTime(hours, minutes, seconds);
    }

    public String display() {
        BerlinClockTimeTextRepresentation berlinClockTimeRepresentation = new BerlinClockTimeTextRepresentation(
                berlinClockTime);
        return berlinClockTimeRepresentation.getRepresentation();
    }

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println(USAGE_INFO);
            return;
        }

        try {
            BerlinClockTimeController controller = new BerlinClockTimeController(args[0]);
            System.out.println(controller.display());
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            System.out.println(USAGE_INFO);
        }
    }
}
